import { queryBaseSettingInfo } from '@/services/salaryManage/salaryCalculate';
import { orgService } from '@/services/salaryManage/orgService';
import { isSuccess } from '@/utils/request';
import { sessionStorageUtil } from '@/pages/SalaryManage/utils/storageUtil';
import { getCurrentCompanyCode } from '@/pages/SalaryManage/utils'

const actionType = {
    INIT: 'init',
    SET_BASE_SETTING: 'setBaseSetting',
    FETCH_BASE_SETTING: 'fetchBaseSetting',
    CLEAR: 'clear',
    SET_MULTI_STORE: 'setMultiStore',
    SET_COMPANY_LIST: 'setCompanyList',
    FETCH_COMPANY_LIST: 'fetchCompanyList'
};

// 财务、审批详情、审批流程 模块
const financeReg = /(\/)/gi;
const loginAction = /\/login/gi;
const namespace = 'finance';

export default {
    namespace,

    state: {
        loaded: {
            baseSetting: false,
            companySimpleList: false
        },
        baseSetting: {},
        companySimpleList: null
    },

    reducers: {
        [actionType.SET_BASE_SETTING](state, { payload }) {
            const { loaded } = state;

            return {
                ...state,
                baseSetting: {
                    ...payload
                },
                loaded: {
                    ...loaded,
                    baseSetting: true
                }
            };
        },

        [actionType.SET_COMPANY_LIST](state, { payload }) {
            const { loaded } = state;

            return {
                ...state,
                companySimpleList: [
                    ...payload
                ],
                loaded: {
                    ...loaded,
                    companySimpleList: true
                }
            };
        },

        [actionType.CLEAR](state) {
            sessionStorageUtil.removeItemInFinance('companySimpleList');
            return {
                ...state,
                loaded: {
                    baseSetting: false,
                    companySimpleList: false
                },
                baseSetting: null,
                companySimpleList: null
            };
        },

        [actionType.SET_MULTI_STORE](state, action) {
            return {
                ...state,
                ...action.payload
            };
        }
    },

    effects: {
        *[actionType.FETCH_BASE_SETTING](state, { call, put }) {
            const response = yield call(queryBaseSettingInfo, getCurrentCompanyCode());

            if (!isSuccess(response)) {
                return;
            }

            yield put({
                type: actionType.SET_BASE_SETTING,
                payload: response.data
            });
        },

        *[actionType.FETCH_COMPANY_LIST](state, { call, put }) {
            const useCache = state?.payload?.useCache;

            let companySimpleList = null;
            if (useCache) {
                companySimpleList = sessionStorageUtil.getItemInFinance('companySimpleList');
            }

            if (companySimpleList == null) {
                const response = yield call(orgService.queryCompanySimpleList);
                if (!isSuccess(response)) {
                    return;
                }
                companySimpleList = response.data;
                sessionStorageUtil.setItemInFinance('companySimpleList', companySimpleList);
            }

            yield put({
                type: actionType.SET_COMPANY_LIST,
                payload: companySimpleList
            });
        },

        *init(state, { put, select }) {
            const loaded = yield select(s => s[namespace].loaded);

            if (!loaded.baseSetting) {
                yield put({
                    type: actionType.FETCH_BASE_SETTING
                });
            }

            if (!loaded.companySimpleList) {
                yield put({
                    type: actionType.FETCH_COMPANY_LIST,
                    payload: { useCache: true }
                });
            }
        }
    },

    subscriptions: {
        setup(props) {
            const { dispatch, history } = props;
            return history.listen(({ pathname }) => {
                // 初始化参数配置
                if (String(pathname).search(financeReg) > -1 && getCurrentCompanyCode()) {
                    dispatch({
                        type: actionType.INIT
                    });
                } else if (String(pathname).search(loginAction) > -1) { // 如果是登录登出操作清空缓存
                    dispatch({
                        type: actionType.CLEAR
                    });
                }
            });
        }
    }
};
